package com.yidu.commom;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 类的描述-- 随机数生成工具类
 * <p>随机数生成工具类,主要包括<br> 
 *  中文姓名，性别，Email，手机号，住址  
 * @author 楠竹
 * @date 2018年7月13日
 * @version 1.0
 */
public class RandomUtil {


	public static final String base = "abcdefghijklmnopqrstuvwxyz0123456789";     
	private static final String firstName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续";    
	private static final String girl="秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";    
	public static final String boy="伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";    
	public static final String[] email_suffix="@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(",");    

	/**
	 * 随机数字 <br>
	 * 从开始数字 与结束数字随机
	 * @param start 开始数字
	 * @param end 结束数字
	 * @return
	 */
	public static int getNum(int start,int end) {    
		return (int)(Math.random()*(end-start+1)+start);    
	}  

	/***
	 * 
	 * Project Name: recruit-helper-util
	 * <p>随机生成Email 
	 *
	 * @author youqiang.xiong
	 * @date 2018年5月23日  下午2:13:06
	 * @version v1.0
	 * @since 
	 * @param lMin
	 *         最小长度  
	 * @param lMax
	 *         最大长度
	 * @return
	 */
	public static String getEmail(int lMin,int lMax) {    
		int length=getNum(lMin,lMax);    
		StringBuffer sb = new StringBuffer();         
		for (int i = 0; i < length; i++) {         
			int number = (int)(Math.random()*base.length());    
			sb.append(base.charAt(number));         
		}    
		sb.append(email_suffix[(int)(Math.random()*email_suffix.length)]);    
		return sb.toString();       
	}    

	//电话号码 前三位数字
	private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");    

	/***
	 * 
	 * Project Name: recruit-helper-util
	 * <p>随机生成手机号码
	 *
	 * @author youqiang.xiong
	 * @date 2018年5月23日  下午2:14:17
	 * @version v1.0
	 * @since 
	 * @return
	 */
	public static String getTelephone() {    
		int index=getNum(0,telFirst.length-1);    
		String first=telFirst[index];    
		String second=String.valueOf(getNum(1,888)+10000).substring(1);    
		String thrid=String.valueOf(getNum(1,9100)+10000).substring(1);    
		return first+second+thrid;    
	}    

	/***
	 * 
	 * Project Name: recruit-helper-util
	 * <p>随机生成8位电话号码
	 *
	 * @author youqiang.xiong
	 * @date 2018年5月23日  下午2:15:31
	 * @version v1.0
	 * @since 
	 * @return
	 */
	public static String getLandline() {    
		int index=getNum(0,telFirst.length-1);    
		String first=telFirst[index];    
		String second=String.valueOf(getNum(1,888)+10000).substring(1);    
		String thrid=String.valueOf(getNum(1,9100)+10000).substring(1);    
		return first+second+thrid;    
	}  



	/**  
	 * 返回中文性别
	 */    
	public static String name_sex = "";    

	/***
	 * 
	 * Project Name: recruit-helper-util
	 * <p>返回中文姓名
	 *
	 * @author youqiang.xiong
	 * @date 2018年5月23日  下午2:16:16
	 * @version v1.0
	 * @since 
	 * @return
	 */
	public static String getChineseName() {
		int index = getNum(0, firstName.length() - 1);
		String first = firstName.substring(index, index + 1);
		int sex = getNum(0, 1);
		String str = boy;
		int length = boy.length();
		if (sex == 0) {
			str = girl;
			length = girl.length();
			name_sex = "女";
		} else {
			name_sex = "男";
		}
		index = getNum(0, length - 1);
		String second = str.substring(index, index + 1);
		int hasThird = getNum(0, 1);
		String third = "";
		if (hasThird == 1) {
			index = getNum(0, length - 1);
			third = str.substring(index, index + 1);
		}
		return first + second + third;
	}    

	/**
	 * 随机生成中文用户名
	 * @param len 长度
	 * @return
	 */
	public static String getRandomJianHan(int len) {
		String ret = "";
		for (int i = 0; i < len; i++) {
			String str = null;
			int hightPos, lowPos; // 定义高低位
			Random random = new Random();
			hightPos = (176 + Math.abs(random.nextInt(39))); // 获取高位值
			lowPos = (161 + Math.abs(random.nextInt(93))); // 获取低位值
			byte[] b = new byte[2];
			b[0] = (new Integer(hightPos).byteValue());
			b[1] = (new Integer(lowPos).byteValue());
			try {
				str = new String(b, "GBK"); // 转成中文
			} catch (UnsupportedEncodingException ex) {
				ex.printStackTrace();
			}
			ret += str;
		}
		return ret;
	}


	/**
	 * 生成随机用户名，数字和字母组成,  
	 * @param length
	 * @return
	 */
	public static String getStringRandom(int length) {  

		String val = "";  
		Random random = new Random();  

		//参数length，表示生成几位随机数  
		for(int i = 0; i < length; i++) {  

			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
			//输出字母还是数字  
			if( "char".equalsIgnoreCase(charOrNum) ) {  
				//输出是大写字母还是小写字母  
				int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
				val += (char)(random.nextInt(26) + temp);  
			} else if( "num".equalsIgnoreCase(charOrNum) ) {  
				val += String.valueOf(random.nextInt(10));  
			}  
		}  
		return val;  
	}      

	/**
	 * 利用java自带工具包MessageDigest 实现	MD5加密
	 * @param str
	 * @return
	 */
	public static String randomMd5(String str){
		String result = "";
		 try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			 
			 result = MD5Utils.stringToMD5(str);
			
			
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 return result;
		 
		
		
	}
	
	//内部类
	static class MD5Utils{
		 public static String stringToMD5(String plainText) {
		        byte[] secretBytes = null;
		        try {
		            secretBytes = MessageDigest.getInstance("md5").digest(
		                    plainText.getBytes());
		        } catch (NoSuchAlgorithmException e) {
		            throw new RuntimeException("没有这个md5算法！");
		        }
		        String md5code = new BigInteger(1, secretBytes).toString(16);
		        for (int i = 0; i < 32 - md5code.length(); i++) {
		            md5code = "0" + md5code;
		        }
		        return md5code;
		    }
		 
	}
	

	public static void main(String[] args) {

		//int num = getNum(2, 5);
		// String str = getEmail(2, 11);
		//String str = getTelephone();
		//String str = getChineseName();
		//String str  = getLandline();
		//String str  =getStringRandom(8);
		// String str = getRandomJianHan(8);


		/* List<String> list = new ArrayList<>();
		  for (int i = 0; i < 12; i++) {

			  list.add(getTelephone());
		}

		  for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}*/

		 List<String> list = new ArrayList<>();
		for (int i = 0; i < 13; i++) {
			list.add(getRandomJianHan(5));
		}

		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		
		/*String str = randomMd5("O8HeQR7L");

		System.out.println(str);*/
	}


}
